package com.hxzy.tms.aciton;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import com.hxzy.tms.entity.Project;
import com.hxzy.tms.entity.Team;
import com.hxzy.tms.service.ServiceResult;
import com.hxzy.tms.service.TeamService;

public class ProjectAction extends AbstractAction {
    private static final long serialVersionUID = -7738689473106266928L;

    private Project project;
    private TeamService teamService;
    private String teamId;

    public String post() throws Exception {
        if (StringUtils.isBlank(teamId) || !NumberUtils.isDigits(teamId)) {
            addActionError("参数错误");
            return ERROR;
        }
        long _teamId = new Long(teamId);
        Team team = teamService.read(_teamId).data;
        if (team == null) {
            addActionError("团队不存在或者已被删除");
            return ERROR;
        }
        assign("team", team);
        if (isGET()) {
            return INPUT;
        }

        if (isPOST()) {
            if (project == null || StringUtils.isBlank(project.getTitle())) {
                addFieldError("project.title", "项目名称不能为空");
                return INPUT;
            }
            if (project == null || StringUtils.isBlank(project.getDesc())) {
                addFieldError("project.desc", "项目名称不能为空");
                return INPUT;
            }

            ServiceResult<Project> result = teamService.create(team.getId(), project.getTitle(), project.getDesc());
            if (result.status == 1) {
                addActionMessage("保存成功");
                return SUCCESS;
            } else {
                addActionError(result.message);
                return ERROR;
            }
        }

        return INPUT;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public TeamService getTeamService() {
        return teamService;
    }

    public void setTeamService(TeamService teamService) {
        this.teamService = teamService;
    }

    public String getTeamId() {
        return teamId;
    }

    public void setTeamId(String teamId) {
        this.teamId = teamId;
    }

}
